﻿#region UsingDirectives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml; 
#endregion

namespace CameraRecorder
{
    public partial class F_CameraSettings : Form
    {
        #region Members variables
        public string SourceURL { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool RequireLogon { get; set; }
        public bool SaveCapture { get; set; }
        public string PathSaveCapture { get; set; }
        public int FrameInterval { get; set; }
        #endregion

        #region Constructor
        public F_CameraSettings()
        {
            InitializeComponent();
        } 
        #endregion

        #region Form Events	
        private void F_CameraSettings_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SourceURL))
            {
                TB_cameraAddress.Text = SourceURL;
            }
            if (RequireLogon)
            {
                CB_requireLogon.Checked = true;
            }
            else
            {
                CB_requireLogon.Checked = false;
                TB_userName.Enabled = false;
                TB_password.Enabled = false;
            }
            if (!string.IsNullOrEmpty(Password))
            {
                TB_password.Text = Password;
            }
            if (!string.IsNullOrEmpty(UserName))
            {
                TB_userName.Text = UserName;
            }
            if (SaveCapture)
            {
                CB_saveCapture.Checked = true;
            }
            else
            {
                CB_saveCapture.Checked = false;
                TB_pathToSaveCapture.Enabled = false;
                B_selectCaptureDestinationFolder.Enabled = false;
            }
            if (!string.IsNullOrEmpty(PathSaveCapture))
            {
                TB_pathToSaveCapture.Text = PathSaveCapture;
            }
            if (FrameInterval != 0)
            {
                MTB_fInterval.Text = FrameInterval.ToString();
            }
        }

        private void B_apply_Click(object sender, EventArgs e)
        {
            SourceURL = TB_cameraAddress.Text;
            if (!string.IsNullOrEmpty(MTB_fInterval.Text))
            {
                FrameInterval = int.Parse(MTB_fInterval.Text);
            }
            if (RequireLogon)
            {
                UserName = TB_userName.Text;
                Password = TB_password.Text;
            }
            else
            {
                UserName = string.Empty;
                Password = string.Empty;
            }
            if (SaveCapture)
            {
                PathSaveCapture = TB_pathToSaveCapture.Text;
            }
            else
            {
                PathSaveCapture = string.Empty;
            }

            XmlTextWriter textWriter = new XmlTextWriter("cameraSettings.xml", null);
            textWriter.WriteStartDocument();
            textWriter.WriteComment("Simple IP Camera Recorder - file settings");
            textWriter.WriteStartElement("CameraSettings");
            textWriter.WriteStartElement("SourceURL", SourceURL);
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("FramePerMinute", FrameInterval.ToString());
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("RequireLogon", RequireLogon.ToString());
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("UserName", UserName);
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("Password", Password);
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("SaveCapture", SaveCapture.ToString());
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("PathSaveCapture", PathSaveCapture);
            textWriter.WriteEndElement();
            textWriter.WriteEndDocument();
            textWriter.Close();

            this.Close();
        }

        private void B_ignoreAndCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CB_requireLogon_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_requireLogon.Checked == true)
            {
                RequireLogon = true;
                TB_userName.Enabled = true;
                TB_password.Enabled = true;
            }
            else
            {
                RequireLogon = false;
                TB_userName.Enabled = false;
                TB_password.Enabled = false;
            }
        }

        private void CB_saveCapture_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_saveCapture.Checked == true)
            {
                SaveCapture = true;
                TB_pathToSaveCapture.Enabled = true;
                B_selectCaptureDestinationFolder.Enabled = true;
            }
            else
            {
                SaveCapture = false;
                TB_pathToSaveCapture.Enabled = false;
                B_selectCaptureDestinationFolder.Enabled = false;
            }
        }

        private void B_selectCaptureDestinationFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                TB_pathToSaveCapture.Text = fbd.SelectedPath;
            }
        }
        #endregion
    }
}
